﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Design;
using System.ComponentModel;

using Qreed.Windows.ComponentModel;
using System.Windows.Forms;
using System.Drawing;

namespace GotLyrics
{
    public class LyricsUITypeEditor : UITypeEditor
    {
        private IWin32Window _parent;
        private static Font _modifiedFont;
        private static Font _normalFont;

        public LyricsUITypeEditor(IWin32Window parent)
        {
            _parent = parent;

            if (_modifiedFont == null)
            {
                Control c = Control.FromHandle(parent.Handle);
                _normalFont = c.Font;
                _modifiedFont = new Font(_normalFont, _normalFont.Style | FontStyle.Bold);
            }
        }

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            
            return UITypeEditorEditStyle.Modal;
        }

        public override bool GetPaintValueSupported(ITypeDescriptorContext context)
        {
            return false;
            //return base.GetPaintValueSupported(context);
        }

        public override void PaintValue(PaintValueEventArgs e)
        {
            PropertyBagItemDescriptor descriptor = (PropertyBagItemDescriptor)e.Context.PropertyDescriptor;
            PropertyBagItem item = descriptor.Item;

            bool isModified = false;
            bool hasLyrics = false;
            bool isMultiValue = false;

            if (item is PropertyBagExpandableItem)
            {
                PropertyBagExpandableItem expandableItem = (PropertyBagExpandableItem)item;

                if (!string.IsNullOrEmpty((string)expandableItem.Value))
                    hasLyrics = true;

                if (((string)expandableItem.Value) == expandableItem.MultipleValuesText)
                    isMultiValue = true;

                object[] objects = (object[])expandableItem.Tag;

                foreach (AudioFileContext audioFile in objects)
                {
                    if (audioFile.IsModified)
                    {
                        isModified = true;
                        break;
                    }
                }
            }
            else
            {
                AudioFileContext audioFile = (AudioFileContext)item.Tag;

                isModified = audioFile.IsModified;

                if (!string.IsNullOrEmpty(audioFile.Tags.UnsyncedLyrics))
                    hasLyrics = true;
            }

            if (hasLyrics)
            {
                if (isMultiValue)
                {
                    using (SolidBrush b = new SolidBrush(Color.LightGray))
                    {
                        e.Graphics.FillRectangle(b, e.Bounds);
                    }
                }
                
                Color c = isModified ? Color.Black : Color.FromArgb(120, 120, 120);
                using (Pen p = new Pen(c))
                {
                    Random rnd = new Random(e.Value.GetHashCode());

                    for (int offset = e.Bounds.Top + 2; offset < e.Bounds.Height; offset += 2)
                    {
                        int x1 = e.Bounds.Width >> 2;
                        int x2 = e.Bounds.Width - x1 + 1 - rnd.Next(x1);
                        
                        int y1 = offset;
                        int y2 = y1;

                        e.Graphics.DrawLine(p, x1, y1, x2, y2);
                    }
                }

                /*Font font = (isModified ? _modifiedFont : _normalFont);

                using(SolidBrush b = new SolidBrush(Color.Black))
                {
                    e.Graphics.DrawString("-*-", font, b, e.Bounds);
                }*/
            }
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            PropertyBagItemDescriptor descriptor = (PropertyBagItemDescriptor)context.PropertyDescriptor;
            PropertyBagItem item = descriptor.Item;

            LyricsPropertiesDialog dlg = new LyricsPropertiesDialog();

            DialogResult res = dlg.ShowDialog(_parent);

            descriptor.SetValue(item, "Jajaja");
            return "Yup!";
            // return base.EditValue(context, provider, value);
        }
        
    }
}
